﻿using HDJ.Framework.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace HDJ.Framework.Core.Editor
{
    public static class TableDataDrawFieldController
    {
        /// <summary>
        /// 默认不使用任何功能标签的tag
        /// </summary>
        public const string DefaultTag = "None";

        private static Dictionary<Type, Dictionary<string, TableDataDrawFieldBase>> objectList = new Dictionary<Type, Dictionary<string, TableDataDrawFieldBase>>();
        private static Dictionary<Type, List<string>> allTags = new Dictionary<Type, List<string>>();
        private static TableDataDrawFieldBase defaultDrawObject = null;
        private static bool isInit = false;
        private static void Init()
        {
            if (isInit)
                return;
            isInit = true;
            defaultDrawObject = new TableDataDrawFieldBase();
            objectList.Clear();
            Type[] types = ReflectionUtils.GetChildTypes(typeof(TableDataDrawFieldBase));
            foreach (var t in types)
            {
                TableDataDrawFieldBase obj = (TableDataDrawFieldBase)ReflectionUtils.CreateDefultInstance(t);
                obj.Init();
                Dictionary<string, TableDataDrawFieldBase> tempDic = null;
                if (objectList.ContainsKey(obj.FieldType))
                {
                    tempDic = objectList[obj.FieldType];
                }
                else
                {
                    tempDic = new Dictionary<string, TableDataDrawFieldBase>();
                    objectList.Add(obj.FieldType, tempDic);
                }
                if (!tempDic.ContainsKey(obj.Tag))
                {
                    tempDic.Add(obj.Tag, obj);
                }

            }
            allTags.Clear();
            foreach (var item in objectList)
            {
                List<string> tags = new List<string>(item.Value.Keys);
                tags.Add(DefaultTag);
                allTags.Add(item.Key, tags);
            }

        }
        public static void ReInit()
        {
            isInit = false;

            Init();
        }
        /// <summary>
        /// 是否需要特殊绘制
        /// </summary>
        /// <param name="fieldType"></param>
        /// <param name="tag"></param>
        /// <returns></returns>
        public static bool HasTableDataSpecialDrawField(Type fieldType, string tag)
        {
            Init();
            if (string.IsNullOrEmpty(tag))
                return false;
           // Debug.Log("fieldType:" + fieldType + " tag:" + tag);
            if (objectList.ContainsKey(fieldType) && objectList[fieldType].ContainsKey(tag))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static TableDataDrawFieldBase GetTableDataDrawField(Type fieldType, string tag)
        {
            Init();

            if (objectList.ContainsKey(fieldType) && objectList[fieldType].ContainsKey(tag))
            {
                return objectList[fieldType][tag];
            }
            else
            {
                return defaultDrawObject;
            }
        }

        public static List<string> GetTags(Type fieldType)
        {
            List<string> tags = null;
            if (allTags.ContainsKey(fieldType))
            {
                tags = allTags[fieldType];
            }
            else
            {
                tags = new List<string>();
                tags.Add(DefaultTag);
            }
            return tags;
        }
    }
}

